<?php


namespace app\controller;
use app\model\User;
use app\tools\GetTelTool;
use app\tools\ResponseTool;
use app\validate\user\UserLogin;
use think\exception\ValidateException;
use think\facade\Cache;
use think\facade\Log;
use think\facade\Request;


class Register extends MyBaseController
{
    public function register () {
        $userInfo = Request::param();
        $user = new User(Request::param());
        if (!User::where('user_name', $userInfo['userName'])->findOrEmpty()->isEmpty())
        {
            return ResponseTool::getRs('PARAM_ERROR', [], '用户名已存在');
        }
        else if (!User::where('tel_num', $userInfo['telNum'])->findOrEmpty()->isEmpty())
        {
            return ResponseTool::getRs('PARAM_ERROR', [], '手机号已存在');
        }
//        else if (!Cache::get($userInfo['telNum']) || $userInfo['validateCode'] != Cache::get($userInfo['telNum']))
//        {
//            return ResponseTool::getRs('PARAM_ERROR', [], '短信验证码过期或失效');
//        }
        else {
            $insert = new User();
            $insert->save(
                [
                    'user_name'     =>  $user->userName,
                    'user_password' =>  $user->userPassword,
                    'tel_num'       =>  $user->telNum,
                ]
            );
            Log::info($insert);
            if ($insert) {
                return ResponseTool::getRs('OK', [], '注册成功');
            } else {
                return ResponseTool::getRs('OTHERS', [], '服务器忙');
            }
        }
    }
}